package com.hisysteTopet.hisystepet.interceptors;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.InvalidClaimException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hisysteTopet.hisystepet.controller.LoginController;
import com.hisysteTopet.hisystepet.utils.JWTUtils;
import com.hisysteTopet.hisystepet.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Chilly Cui on 2020/9/9.
 */
@Slf4j
@Component
public class JWTInterceptor implements HandlerInterceptor {

    @Autowired
    LoginController loginController;

    public static Boolean tokenState;

    public static String username ;

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {

        //获取请求头中的令牌
        String token = request.getHeader("token");
//        System.out.println("head"+token);
        String Token = LoginController.TOKEN;
        String userToken = TokenUtils.getTokenUsername(Token);  // 原本的
        log.info("我是userToken当前token为：{}", userToken);
        String tokenUsername = TokenUtils.getTokenUsername(token);// 传过来的
        System.out.println(userToken+"jwt"+tokenUsername);
        Map<String, Object> map = new HashMap<>();
        try {
            username = tokenUsername;
            tokenState = userToken.equals(username);
            if (tokenState){
//                JWTUtils.verify(token);
                return true;
            }else {
                return false;
            }
        } catch (SignatureVerificationException e) {
            e.printStackTrace();
            map.put("msg", "签名不一致");
        } catch (TokenExpiredException e) {
            e.printStackTrace();
            map.put("msg", "令牌过期");
        } catch (AlgorithmMismatchException e) {
            e.printStackTrace();
            map.put("msg", "算法不匹配");
        } catch (InvalidClaimException e) {
            e.printStackTrace();
            map.put("msg", "失效的payload");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "token无效");
        }

        map.put("state", false);

        //响应到前台: 将map转为json
        String json = new ObjectMapper().writeValueAsString(map);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(json);
        return false;
    }
}
